package com.ruoyi.life.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.life.domain.FeApartmentCancelBill;
import com.ruoyi.life.mapper.FeApartmentCancelBillMapper;
import com.ruoyi.life.service.IFeApartmentCancelBillService;
import com.ruoyi.life.service.manager.BillManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公寓退租账单;Service业务层处理
 *
 * @author cjie
 * @date 2022-09-26
 */
@Service
public class FeApartmentCancelBillServiceImpl implements IFeApartmentCancelBillService {
    @Autowired
    private FeApartmentCancelBillMapper feApartmentCancelBillMapper;

    @Autowired
    private BillManager billManager;

    /**
     * 查询公寓退租账单
     *
     * @param id 公寓退租账单ID
     * @return 公寓退租账单
     */
    @Override
    public FeApartmentCancelBill selectFeApartmentCancelBillById(String id) {
        return feApartmentCancelBillMapper.selectFeApartmentCancelBillById(id);
    }

    /**
     * 查询公寓退租账单列表
     *
     * @param feApartmentCancelBill 公寓退租账单
     * @return 公寓退租账单
     */
    @Override
    public List<FeApartmentCancelBill> selectFeApartmentCancelBillList(FeApartmentCancelBill feApartmentCancelBill) {
        return feApartmentCancelBillMapper.selectFeApartmentCancelBillList(feApartmentCancelBill);
    }

    /**
     * 新增公寓退租账单;
     *
     * @param apartmentCancelFee 公寓退租账单;
     * @return 结果
     */
    @Override
    public int insertFeApartmentCancelBill(FeApartmentCancelBill apartmentCancelFee) {
        apartmentCancelFee.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        apartmentCancelFee.setCreateTime(DateUtils.getNowDate());
        apartmentCancelFee.setCreateBy(SecurityUtils.getUsername());
        //apartmentCancelFee.set
        //设置租赁的流水账号
        String number = billManager.genLeaseNumber("CR");
        apartmentCancelFee.setBillNum(number);
        apartmentCancelFee.setBillState("0");
        return feApartmentCancelBillMapper.insertFeApartmentCancelBill(apartmentCancelFee);
    }

    /**
     * 修改公寓退租账单
     *
     * @param feApartmentCancelBill 公寓退租账单
     * @return 结果
     */
    @Override
    public int updateFeApartmentCancelBill(FeApartmentCancelBill apartmentCancelFee) {
        apartmentCancelFee.setUpdateTime(DateUtils.getNowDate());
        apartmentCancelFee.setUpdateBy(SecurityUtils.getUsername());
        //确认后开始计算违约金，
        FeApartmentCancelBill old = this.selectFeApartmentCancelBillById(apartmentCancelFee.getId());
        if(old.getBillState().equals("0") && apartmentCancelFee.getBillState().equals("1")){
            apartmentCancelFee.setEffectiveDate(DateUtils.getNowDate());
        }
        return feApartmentCancelBillMapper.updateFeApartmentCancelBill(apartmentCancelFee);
    }

    /**
     * 批量删除公寓退租账单
     *
     * @param ids 需要删除的公寓退租账单ID
     * @return 结果
     */
    @Override
    public int deleteFeApartmentCancelBillByIds(String[] ids) {
        return feApartmentCancelBillMapper.deleteFeApartmentCancelBillByIds(ids);
    }

    /**
     * 删除公寓退租账单信息
     *
     * @param id 公寓退租账单ID
     * @return 结果
     */
    @Override
    public int deleteFeApartmentCancelBillById(String id) {
        return feApartmentCancelBillMapper.deleteFeApartmentCancelBillById(id);
    }

    @Override
    public int updateFeApartmentCancelBillState(FeApartmentCancelBill feApartmentCancelBill) {
        if(feApartmentCancelBill.getPayState().equals("1")){
            feApartmentCancelBill.setEffectiveDate(DateUtils.getNowDate());
        }
        return feApartmentCancelBillMapper.updateFeApartmentCancelBill(feApartmentCancelBill);
    }

}
